import http from '../../../http/http';
import { useEffect, useState } from 'react';
import { View, Text, RadioGroup, Label, Radio, Button } from '@tarojs/components'
import './myAddress.less'
// 单选按钮
import { AtRadio } from 'taro-ui'
import "taro-ui/dist/style/components/radio.scss";
import "taro-ui/dist/style/components/icon.scss";
import Taro from '@tarojs/taro'
// button按钮
import "taro-ui/dist/style/components/button.scss";
import "taro-ui/dist/style/components/loading.scss";
function myAddress() {
    //   地址数据

    // const W_F_myaddress = [
    //     {   
    //         id:1,
    //         name: '王方华',
    //         cellphone: '13094598205',
    //         address: '成都市高新区天府三街国信安培训中心',
    //         checked: 'true'
    //     },
    //     {   
    //         id:2,
    //         name: '王方华',
    //         cellphone: '13094598205',
    //         address: '成都市高新区天府三街国信安培训中心'
    //     },
    //     {   
    //         id:3,
    //         name: '王方华',
    //         cellphone: '13094598205',
    //         address: '成都市高新区天府三街国信安培训中心'
    //     },
    //     {   
    //         id:4,
    //         name: '王方华',
    //         cellphone: '13094598205',
    //         address: '成都市高新区天府三街国信安培训中心'

    //     },

    // ]
    const [W_F_myaddress, setW_F_myaddress] = useState([])
    const tapEvent = () => {
     
    }
    const radioChange = (e) => {
        const checked = e.detail.value
        // console.log(e.detail.value)
        const changed = {}
        for (let i = 0; i < this.data.radioItems.length; i++) {
            if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
                changed['radioItems[' + i + '].checked'] = true
            } else {
                changed['radioItems[' + i + '].checked'] = false
            }
        }
        this.setData(changed)
    }
    //   地址查询接口
    const myaddress = () => {
        http({
            url: '/wangfanghua/c_myaddress',
            method: 'get',
            data: {
                id: 1
            }
        }).then((res) => {
           
            if (res.code == 200) {
                setW_F_myaddress(res.data)
            }
        })
    }
    // 删除
    const addressshanchu = (id) => {
       
        http({
            url:'/wangfanghua/c_addressdel',
            method:'post',
            data:{
                id:id
            }
        }).then((res)=>{
            if(res.code==200)myaddress()
        })
    }
    // 加载
    useEffect(() => {
        myaddress()
    }, [])
    // 地址渲染
    const W_F_addressdizhi = () => {
        return (
            W_F_myaddress.map((v, i) => {
                // console.log(v,i)
                return (
                    <View className="W_F_listnamrorder">
                        <Label
                            className='example-body__label' for='1' key={i}>
                            <Radio onChange={radioChange} checked={v.defaultt == 1 ? true : false} className="W_F_lobel" id={v.id} value={v}> </Radio></Label>
                        <View className="W_F_dizhixiangqing">
                            <View className="W_F_listorderlie"><View><Text className="W_F_namexingming">收件人：</Text><Text className="W_F_xingxixiangxi">{v.user_name}</Text></View><Text onClick={addressshanchu.bind(this,v.id)}>删除</Text></View>
                            <View className="W_F_listorderlie"><View><Text className="W_F_namexingming">手机号：</Text><Text className="W_F_xingxixiangxi">{v.tel}</Text></View></View>
                            <View className="W_F_listorderlie"><View><Text className="W_F_namexingming">地址详情：</Text><Text className="W_F_xingxixiangxi">{v.order_address}</Text></View></View>
                        </View>

                        {/* <AtButton>设置</AtButton> */}


                    </View>
                )
            })
        )
    }
    // 添加地址的跳转
    const W_F_AddAddress = () => {
        Taro.redirectTo({
            url: '/pages/mine/addAddress/addAddress'
        })
    }
    return (
        <View className="W_F_box">
            <View className="W_F_box2">
                <View className="W_F_mywodeshoucang">
                    <Text className="W_F_t1">我的地址</Text>
                </View>
                <View >
                    <View>
                        <RadioGroup>
                            <View className="W_F_dizhixingxi">
                                {W_F_addressdizhi()}
                            </View>
                        </RadioGroup>
                    </View>
                </View>
                <View onClick={W_F_AddAddress} className="W_F_buttontt"><Button id="W_F_button" type='primary'>添加地址</Button></View>
            </View>
        </View>

    )

}

export default myAddress;